﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_2._2.Models;
using PagedList;
using PagedList.Mvc;
using Project_2._2.ViewModels;

namespace Project_2._2.Controllers
{
    public class XeController : Controller
    {
        private QuanLyThueXeEntities db = new QuanLyThueXeEntities();

        #region chi tiet xe
        // GET: /Xe/

        public ActionResult ChiTiet( int id)
        {
            return View();
        }

        #endregion

        #region danh sach xe theo dich vu
        public ActionResult DichVu( int? page , int id)
        {
            ViewBag.id = id;

            List<CarList_ViewModel> listcar = new List<CarList_ViewModel>();

            var cars = (from xe in db.Xes
                        join gia in db.GiaXes on xe.XeId equals gia.XeId
                        join dv in db.DichVuThueXes on gia.DichVuId equals dv.DichVuId
                        join loai in db.LoaiXes on xe.LoaiXeId equals loai.LoaiXeId
                        join th in db.ThuongHieux on xe.ThuongHieuId equals th.ThuongHieuId
                        where gia.DichVuId == id
                        select new
                        {
                            id = xe.XeId,
                            name = xe.TenXe,
                            THieu = th.TenThuongHieu,
                            Doixe = xe.DoiXe,
                            Color = xe.MauXe,
                            Loai = loai.TenLoaiXe,
                            Gia = gia.Gia,
                            Hinh = xe.HinhAnhDaiDien,
                            tenDV =  dv.TenDichVu
                        }).ToList();
            CarList_ViewModel carslist;
            if (cars.Count > 0)
            {
                foreach (var item in cars)
                {
                    carslist = new CarList_ViewModel();
                    carslist.id = item.id;
                    carslist.name = item.name;
                    carslist.THieu = item.THieu;
                    carslist.DoiXe = item.Doixe;
                    carslist.Color = item.Color;
                    carslist.Loai = item.Loai;
                    carslist.Gia = item.Gia;
                    carslist.hinh = item.Hinh;
                    listcar.Add(carslist);
                }
                var carReturn = listcar.ToPagedList(page ?? 1, 2);
                ViewBag.TieuDe = cars[0].tenDV;
                return View(carReturn);
            }
            
            return View(  );
        }

        #endregion

        #region danh sach xe theo loai xe
        public ActionResult LoaiXe( int? page , int id , int dvu)
        {
            ViewBag.id = id;
            ViewBag.dvu = dvu;
            List<CarList_ViewModel> listcar = new List<CarList_ViewModel>();

            var cars = (from xe in db.Xes
                        join gia in db.GiaXes on xe.XeId equals gia.XeId
                        join dv in db.DichVuThueXes on gia.DichVuId equals dv.DichVuId
                        join loai in db.LoaiXes on xe.LoaiXeId equals loai.LoaiXeId
                        join th in db.ThuongHieux on xe.ThuongHieuId equals th.ThuongHieuId
                        where gia.DichVuId == dvu && xe.LoaiXeId == id
                        select new
                        {
                            id = xe.XeId,
                            name = xe.TenXe,
                            THieu = th.TenThuongHieu,
                            Doixe = xe.DoiXe,
                            Color = xe.MauXe,
                            Loai = loai.TenLoaiXe,
                            Gia = gia.Gia,
                            Hinh = xe.HinhAnhDaiDien
                        }).ToList();
            CarList_ViewModel carslist;
            if (cars.Count > 0)
            {
                foreach (var item in cars)
                {
                    carslist = new CarList_ViewModel();
                    carslist.id = item.id;
                    carslist.name = item.name;
                    carslist.THieu = item.THieu;
                    carslist.DoiXe = item.Doixe;
                    carslist.Color = item.Color;
                    carslist.Loai = item.Loai;
                    carslist.Gia = item.Gia;
                    carslist.hinh = item.Hinh;
                    listcar.Add(carslist);
                }
                var carReturn = listcar.ToPagedList(page ?? 1, 1);
                ViewBag.TieuDe = cars[0].Loai;
                return View(carReturn);
            }

            return View();
            //return View(car);
        }
             
        #endregion
    }
}
